package com.dzzw.Dao;


import com.dzzw.Model.Leader;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//部门领导
public class LeaderDao {
    private final Connection con = DButils.getCon();
    //领导登录
    public List<Leader> LeaderLogin(String leader_actnumber, String leader_password) {
        Leader leader = new Leader();
        List<Leader> list = new ArrayList<>();
        String sql = "select * from t_leader where leader_actnumber=? and leader_password=?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,leader_actnumber);
            preparedStatement.setString(2,leader_password);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                leader.setLeader_id(resultSet.getInt("leader_id"));
                leader.setLeader_actnumber(resultSet.getString("leader_actnumber"));
                leader.setLeader_password(resultSet.getString("leader_password"));
                leader.setLeader_name(resultSet.getString("leader_name"));
                list.add(leader);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
        } catch(Exception e){
            System.out.println("数据查询失败！");
            e.printStackTrace();
        }
        return list;
    }

    //领导信息注册
    public void LeaderAdd(Leader leader) {
        String sql = "insert into t_leader(leader_name,leader_password,leader_actnumber) values(?,?,?)";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, leader.getLeader_name());
            preparedStatement.setString(2, leader.getLeader_password());
            preparedStatement.setString(3, leader.getLeader_actnumber());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch(Exception e) {
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //领导信息修改
    public void LeaderModify(Leader leader) {
        String sql = "update t_leader set leader_actnumber=?,leader_name=?,leader_password=? where leader_id=?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,leader.getLeader_actnumber());
            preparedStatement.setString(2, leader.getLeader_name());
            preparedStatement.setString(3, leader.getLeader_password());
            preparedStatement.setInt(4,leader.getLeader_id());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch(Exception e){
            System.out.println("数据修改失败！");

            e.printStackTrace();
        }
    }

    //领导信息注销
    public void LeaderDelete(int leader_id) {
        String sql = "Delete from t_leader where leader_id = ?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,leader_id);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch(Exception e){
            System.out.println("数据单条删除失败！");

            e.printStackTrace();
        }
    }

    //领导信息查询
    public static List<Leader> FindAllLeader() {

        List<Leader> list = new ArrayList<>();
        String sql = "select * from t_leader";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Leader leader = new Leader();
                leader.setLeader_id(resultSet.getInt(1));
                leader.setLeader_actnumber(resultSet.getString(2));
                leader.setLeader_name(resultSet.getString(3));
                leader.setLeader_password(resultSet.getString(4));
                list.add(leader);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch(Exception e){
            System.out.println("查询所有数据失败！");
            e.printStackTrace();
        }
        return list;
    }

    //根据leader_id查询管理员信息;
    public static List<Leader> FindLeaderById(int leader_id) {

        List<Leader> list = new ArrayList<>();
        String sql = "select * from t_leader where leader_id = ?";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,leader_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Leader leader = new Leader();
                leader.setLeader_id(resultSet.getInt(1));
                leader.setLeader_actnumber(resultSet.getString(2));
                leader.setLeader_name(resultSet.getString(3));
                leader.setLeader_password(resultSet.getString(4));
                list.add(leader);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch(Exception e){
            System.out.println("查询所有数据失败！");
            e.printStackTrace();
        }
        return list;
    }
}
